package com.itheima.shengchanxiaofei2;

/*
消费者

套路
    1.死循环
    2.同步代码块
    3.判断是否到数了
    4.判断是否该吃|生产了

 */
public class Foodie extends Thread{

    private Desk desk;
    //private 数据类型 变量名;

    public Foodie() {
    }

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    public Desk getDesk() {
        return desk;
    }

    public void setDesk(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        //    1.死循环
        while (true){
            //    2.同步代码块
            synchronized (desk.getLOCK()){
                //    3.判断是否到数了
                if(desk.getCount()==0){
                    break;
                }else {
                    //    4.判断是否该吃|生产了
                    if(desk.isFlag()){
                        //吃
                       desk.setFlag(false);

                        System.out.println("吃货正在吃第"+ desk.getCount()+"个");
                       desk.setCount(desk.getCount()-1);
                        desk.getLOCK().notifyAll();//叫厨师做
                    }else{
                        try {
                            desk.getLOCK().wait();
                        } catch (InterruptedException e) {
                        }

                    }
                }
            }
        }
    }
}
